package com.ruoyi.web.controller.business;


import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.business.domain.EmployeeClassificationTrainingSuggestions;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.SysOperLog;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.service.IEmployeeClassificationTrainingSuggestionsService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 人员分类培养建议Controller
 *
 * @author ruoyi
 * @date 2024-11-17
 */
@Api(tags = "人员分类培养建议管理",value = "人员分类培养建议管理", description = "人员分类培养建议管理，包括查询、新增、修改、删除等功能")
@RestController
@RequestMapping("/business/suggestions")
public class EmployeeClassificationTrainingSuggestionsController extends BaseController
{
    @Autowired
    private IEmployeeClassificationTrainingSuggestionsService employeeClassificationTrainingSuggestionsService;

    @Autowired
    private RedisCache redisCache;



    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    @ApiOperation(value = "导入数据", notes = "导入人员分类培养建议数据")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EmployeeClassificationTrainingSuggestions> util = new ExcelUtil<EmployeeClassificationTrainingSuggestions>(EmployeeClassificationTrainingSuggestions.class);
        List<EmployeeClassificationTrainingSuggestions> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = employeeClassificationTrainingSuggestionsService.importData(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    @ApiOperation(value = "导出模板", notes = "导出人员分类培养建议数据模板")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EmployeeClassificationTrainingSuggestions> util = new ExcelUtil<EmployeeClassificationTrainingSuggestions>(EmployeeClassificationTrainingSuggestions.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 查询人员分类培养建议列表
     */
    @PreAuthorize("@ss.hasPermi('business:suggestions:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询人员分类培养建议列表")
    public TableDataInfo list(EmployeeClassificationTrainingSuggestions employeeClassificationTrainingSuggestions)
    {
        startPage();
        List<EmployeeClassificationTrainingSuggestions> list = employeeClassificationTrainingSuggestionsService.selectEmployeeClassificationTrainingSuggestionsList(employeeClassificationTrainingSuggestions);
        return getDataTable(list);
    }

    /**
     * 导出人员分类培养建议列表
     */
    @PreAuthorize("@ss.hasPermi('business:suggestions:export')")
    @Log(title = "人员分类培养建议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出数据", notes = "导出人员分类培养建议数据")
    public void export(HttpServletResponse response, EmployeeClassificationTrainingSuggestions employeeClassificationTrainingSuggestions)
    {
        List<EmployeeClassificationTrainingSuggestions> list = employeeClassificationTrainingSuggestionsService.selectEmployeeClassificationTrainingSuggestionsList(employeeClassificationTrainingSuggestions);
        ExcelUtil<EmployeeClassificationTrainingSuggestions> util = new ExcelUtil<EmployeeClassificationTrainingSuggestions>(EmployeeClassificationTrainingSuggestions.class);
        util.exportExcel(response, list, "人员分类培养建议数据");
    }

    /**
     * 获取人员分类培养建议详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:suggestions:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取详情", notes = "获取人员分类培养建议详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(employeeClassificationTrainingSuggestionsService.selectEmployeeClassificationTrainingSuggestionsById(id));
    }

    /**
     * 新增人员分类培养建议
     */
    @PreAuthorize("@ss.hasPermi('business:suggestions:add')")
    @Log(title = "人员分类培养建议", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增数据", notes = "新增人员分类培养建议")
    public AjaxResult add(@RequestBody EmployeeClassificationTrainingSuggestions employeeClassificationTrainingSuggestions)
    {
        if (redisCache.getCacheObject("lock-data") != null) {
            return warn("正在重跑，请勿删除操作");
        }

        return toAjax(employeeClassificationTrainingSuggestionsService.insertEmployeeClassificationTrainingSuggestions(employeeClassificationTrainingSuggestions));
    }

    /**
     * 修改人员分类培养建议
     */
    @PreAuthorize("@ss.hasPermi('business:suggestions:edit')")
    @Log(title = "基础设置1", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改数据", notes = "修改人员分类培养建议")
    public AjaxResult edit(@RequestBody EmployeeClassificationTrainingSuggestions employeeClassificationTrainingSuggestions)
    {
        if (redisCache.getCacheObject("lock-data") != null) {
            return warn("正在重跑，请勿删除操作");
        }

        return toAjax(employeeClassificationTrainingSuggestionsService.updateEmployeeClassificationTrainingSuggestions(employeeClassificationTrainingSuggestions));
    }

    /**
     * 删除人员分类培养建议
     */
    @PreAuthorize("@ss.hasPermi('business:suggestions:remove')")
    @Log(title = "人员分类培养建议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除数据", notes = "删除人员分类培养建议")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(employeeClassificationTrainingSuggestionsService.deleteEmployeeClassificationTrainingSuggestionsByIds(ids));
    }

 /**
     * 批量新增人员分类培养建议
     */
    @PreAuthorize("@ss.hasPermi('business:suggestions:batchAdd')")
    @Log(title = "人员分类培养建议", businessType = BusinessType.UPDATE)
    @PostMapping("/batchAdd")
    @ApiOperation(value = "批量新增数据", notes = "批量新增人员分类培养建议")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "suggestionsList", value = "人员分类培养建议列表", required = true, dataType = "List<EmployeeClassificationTrainingSuggestions>", paramType = "body")
    })
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 400, message = "请求参数有误"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public AjaxResult batchAdd(@RequestBody List<EmployeeClassificationTrainingSuggestions> suggestionsList) {

        long jobPerformance1 = 0;
        long improvementWill1 = 0;
        long jobPerformance2 = 0;
        long improvementWill2 = 0;
        long jobPerformance3 = 0;
        long improvementWill3 = 0;
        long jobPerformance4 = 0;
        long improvementWill4 = 0;

        long oldJobPerformance1 = 0;
        long oldImprovementWill1 = 0;
        long oldJobPerformance2 = 0;
        long oldImprovementWill2 = 0;
        long oldJobPerformance3 = 0;
        long oldImprovementWill3 = 0;
        long oldJobPerformance4 = 0;
        long oldImprovementWill4 = 0;


        for (EmployeeClassificationTrainingSuggestions suggestion : suggestionsList) {

            if(suggestion.getQuadrant().equals("卓越能手")){
                jobPerformance1 = suggestion.getJobPerformance();
                improvementWill1 = suggestion.getImprovementWill();
                EmployeeClassificationTrainingSuggestions oldSuggestion = employeeClassificationTrainingSuggestionsService.selectEmployeeClassificationTrainingSuggestionsById(suggestion.getId());
                if(oldSuggestion != null){
                    oldJobPerformance1 = oldSuggestion.getJobPerformance();
                    oldImprovementWill1 = oldSuggestion.getImprovementWill();
                }

            }

            if(suggestion.getQuadrant().equals("稳健磐石")){
                jobPerformance2 = suggestion.getJobPerformance();
                improvementWill2 = suggestion.getImprovementWill();
                EmployeeClassificationTrainingSuggestions oldSuggestion = employeeClassificationTrainingSuggestionsService.selectEmployeeClassificationTrainingSuggestionsById(suggestion.getId());
                if(oldSuggestion != null){
                    oldJobPerformance2 = oldSuggestion.getJobPerformance();
                    oldImprovementWill2 = oldSuggestion.getImprovementWill();
                }

            }

            if(suggestion.getQuadrant().equals("奋进新星")){
                jobPerformance3 = suggestion.getJobPerformance();
                improvementWill3 = suggestion.getImprovementWill();
                EmployeeClassificationTrainingSuggestions oldSuggestion = employeeClassificationTrainingSuggestionsService.selectEmployeeClassificationTrainingSuggestionsById(suggestion.getId());
                if(oldSuggestion != null){
                    oldJobPerformance3 = oldSuggestion.getJobPerformance();
                    oldImprovementWill3 = oldSuggestion.getImprovementWill();
                }
            }

            if(suggestion.getQuadrant().equals("潜力未燃")){
                jobPerformance4 = suggestion.getJobPerformance();
                improvementWill4 = suggestion.getImprovementWill();
                EmployeeClassificationTrainingSuggestions oldSuggestion = employeeClassificationTrainingSuggestionsService.selectEmployeeClassificationTrainingSuggestionsById(suggestion.getId());
                if(oldSuggestion != null){
                    oldJobPerformance4 = oldSuggestion.getJobPerformance();
                    oldImprovementWill4 = oldSuggestion.getImprovementWill();
                }
            }

            employeeClassificationTrainingSuggestionsService.updateEmployeeClassificationTrainingSuggestions(suggestion);
        }

        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // *========数据库日志=========*//
        SysOperLog operLog = new SysOperLog();
        operLog.setTitle("基础设置");
        operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
        // 请求的地址
        String ip = IpUtils.getIpAddr();
        operLog.setOperIp(ip);
        operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
        if (loginUser != null)
        {
            operLog.setOperName(loginUser.getUsername());
            SysUser currentUser = loginUser.getUser();
            if (StringUtils.isNotNull(currentUser) && StringUtils.isNotNull(currentUser.getDept()))
            {
                operLog.setDeptName(currentUser.getDept().getDeptName());
            }
        }
        // 设置方法名称
        operLog.setMethod("edit");
        // 设置请求方式
        operLog.setRequestMethod(ServletUtils.getRequest().getMethod());

        // 处理设置注解上的参数
        String template = String.format("基础设置 ，" +
                "卓越能手：履职能力大于等于 %d，培训意愿大于等于 %d，" +
                "稳健磐石：履职能力大于等于 %d，培训意愿小于 %d，" +
                "奋进新星：履职能力小于%d，培训意愿大于等于 %d，" +
                "潜力未燃：履职能力小于 %d，培训意愿小于 %d。" +
                "修改为，" +
                "卓越能手：履职能力大于等于 %d，培训意愿大于等于 %d，" +
                "稳健磐石：履职能力大于等于 %d，培训意愿小于 %d，" +
                "奋进新星：履职能力小于 %d，培训意愿大于等于 %d，" +
                "潜力未燃：履职能力小于 %d，培训意愿小于 %d，同时跑了本年度数据",

                oldJobPerformance1, oldImprovementWill1,
                oldJobPerformance2, oldImprovementWill2,
                oldJobPerformance3, oldImprovementWill3,
                oldJobPerformance4, oldImprovementWill4,

                jobPerformance1, improvementWill1,
                jobPerformance2, improvementWill2,
                jobPerformance3, improvementWill3,
                jobPerformance4, improvementWill4
        );
        operLog.setJsonResult(template);
        // 设置消耗时间
        operLog.setCostTime(0L);

        AsyncManager.me().execute(AsyncFactory.recordOper(operLog));

        return toAjax(true);
    }

}
